#!/usr/bin/env bash
# setup-repo.sh
# Used to configure a repo for formatting, and adds a precommit hook to check formatting.
# Copyright 2015 Square, Inc

set -ex
export CDPATH=""
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

pre_commit_file='.git/hooks/pre-commit';

commit_msg_file='.git/hooks/commit-msg';

#判断commit-msg文件是否存在在.git/hooks/路径下
function ensure_commit_msg_file_exists() {

if [ -e "$commit_msg_file" ]; then
return 0
fi

# It's a symlink
if [ -h "$commit_msg_file" ]; then
commit_msg_file=$(readlink "$pre_commit_file")
return 0
fi

if [ -d ".git" ]; then
$(mkdir -p ".git/hooks");
elif [ -e ".git" ]; then
# grab the git dir from our .git file, listed as 'gitdir: blah/blah/foo'
git_dir=$(grep gitdir .git | cut -d ' ' -f 2)
commit_msg_file="$git_dir/hooks/commit-msg"

# Even if our git dir is in an unusual place, we still need to create the hook directory
# if it does not already exist.
$(mkdir -p "$git_dir/hooks");
else
$(mkdir -p ".git/hooks");
fi

if [ -d $commit_msg_file ]; then
return 0
else
cp $DIR/commit_msg/commit-msg $commit_msg_file
fi
}

#添加commit_msg权限
function ensure_commit_msg_file_is_executable() {
$(chmod +x "$commit_msg_file")
}


#判断pre_commit文件是否存在在.git/hooks/路径下
function ensure_pre_commit_file_exists() {
if [ -e "$pre_commit_file" ]; then
return 0
fi
# It's a symlink
if [ -h "$pre_commit_file" ]; then
pre_commit_file=$(readlink "$pre_commit_file")
return 0
fi

if [ -d ".git" ]; then
$(mkdir -p ".git/hooks");
elif [ -e ".git" ]; then
# grab the git dir from our .git file, listed as 'gitdir: blah/blah/foo'
git_dir=$(grep gitdir .git | cut -d ' ' -f 2)
pre_commit_file="$git_dir/hooks/pre-commit"

# Even if our git dir is in an unusual place, we still need to create the hook directory
# if it does not already exist.
$(mkdir -p "$git_dir/hooks");
else
$(mkdir -p ".git/hooks");
fi

$(touch $pre_commit_file)
}

#添加pre_commit权限
function ensure_pre_commit_file_is_executable() {
$(chmod +x "$pre_commit_file")
}

function ensure_hook_is_installed() {
# check if this repo is referenced in the precommit hook already
repo_path=$(git rev-parse --show-toplevel)
if ! grep -q "$repo_path" "$pre_commit_file"; then
echo '#!/usr/bin/env bash
current_repo_path=$(git rev-parse --show-toplevel)
repo_to_format="/Users/zenzz/Workspace/kfang/agent-ios"
if [ "$current_repo_path" == "$repo_to_format" ] && [ -e /Users/zenzz/Workspace/kfang/agent-ios/Pods/oc_clang_format/format-objc-files.sh ]; then /Users/zenzz/Workspace/kfang/agent-ios/Pods/oc_clang_format/format-objc-files.sh -s || exit 1; fi


###
echo "⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️  开始Swift代码修正  ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️"
FILES=$( git diff --cached --diff-filter=d --name-only | grep ".swift$" )

if [ $? -eq 1 ]; then
	echo "No Staged Files For Linting"
	exit 0
fi

Pods/SwiftLint/swiftlint --fix -- $FILES
Pods/SwiftLint/swiftlint lint --strict $FILES
RESULT=$?
git add .
if [ $RESULT -gt 0 ]; then
    echo "⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️  错误信息  ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️"
fi
exit $RESULT' > $pre_commit_file
fi
}

function ensure_git_ignores_clang_format_file() {
grep -q ".clang-format" ".gitignore"
if [ $? -gt 0 ]; then
echo ".clang-format" >> ".gitignore"
echo "spacecommander" >> ".gitignore"
fi
}

function symlink_clang_format() {
$(ln -sf "$DIR/.clang-format" ".clang-format")
}

ensure_pre_commit_file_exists && ensure_pre_commit_file_is_executable && ensure_hook_is_installed && ensure_git_ignores_clang_format_file && symlink_clang_format

ensure_commit_msg_file_exists && ensure_commit_msg_file_is_executable

